package cn.iocoder.yudao.module.train.service.teaching;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.teaching.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.teaching.TeachingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.teaching.TeachingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 授课管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TeachingServiceImpl implements TeachingService {

    @Resource
    private TeachingMapper teachingMapper;

    @Override
    public Long createTeaching(TeachingSaveReqVO createReqVO) {
        // 插入
        TeachingDO teaching = BeanUtils.toBean(createReqVO, TeachingDO.class);
        teachingMapper.insert(teaching);

        // 返回
        return teaching.getId();
    }

    @Override
    public void updateTeaching(TeachingSaveReqVO updateReqVO) {
        // 校验存在
        validateTeachingExists(updateReqVO.getId());
        // 更新
        TeachingDO updateObj = BeanUtils.toBean(updateReqVO, TeachingDO.class);
        teachingMapper.updateById(updateObj);
    }

    @Override
    public void deleteTeaching(Long id) {
        // 校验存在
        validateTeachingExists(id);
        // 删除
        teachingMapper.deleteById(id);
    }

    @Override
        public void deleteTeachingListByIds(List<Long> ids) {
        // 删除
        teachingMapper.deleteByIds(ids);
        }


    private void validateTeachingExists(Long id) {
        if (teachingMapper.selectById(id) == null) {
            throw exception(TEACHING_NOT_EXISTS);
        }
    }

    @Override
    public TeachingDO getTeaching(Long id) {
        return teachingMapper.selectById(id);
    }

    @Override
    public PageResult<TeachingDO> getTeachingPage(TeachingPageReqVO pageReqVO) {
        return teachingMapper.selectPage(pageReqVO);
    }

}